<?php
class JWysiwyg extends CInputWidget {
	public $controls = array (
		"bold" => array (
			"visible" => 'true'
		),
		"italic" => array (
			"visible" => 'true'
		),
		"underline" => array (
			"visible" => 'true'
		),

		"separator00" => array (
			"visible" => 'true'
		),

		"justifyLeft" => array (
			"visible" => 'true'
		),
		"justifyCenter" => array (
			"visible" => 'true'
		),
		"justifyRight" => array (
			"visible" => 'true'
		),
		"justifyFull" => array (
			"visible" => 'true'
		),

		"separator01" => array (
			"visible" => 'true'
		),

		"indent" => array (
			"visible" => 'false'
		),
		"outdent" => array (
			"visible" => 'false'
		),

		"separator02" => array (
			"visible" => 'false'
		),

		"subscript" => array (
			"visible" => 'false'
		),
		"superscript" => array (
			"visible" => 'false'
		),

		"separator03" => array (
			"visible" => 'false'
		),

		"undo" => array (
			"visible" => 'true'
		),
		"redo" => array (
			"visible" => 'true'
		),

		"separator04" => array (
			"visible" => 'true'
		),

		"insertOrderedList" => array (
			"visible" => 'true'
		),
		"insertUnorderedList" => array (
			"visible" => 'true'
		),
		"insertHorizontalRule" => array (
			"visible" => 'false'
		),

		"h1mozilla" => array (
			"visible" => 'false && $.browser.mozilla',
			"className" => 'h1',
			"command" => 'heading',
			"arguments" => array (
				'h1'
			),
			"tags" => array (
				'h1'
			),
			"tooltip" => "Header 1"
		),
		"h2mozilla" => array (
			"visible" => 'false && $.browser.mozilla',
			"className" => 'h2',
			"command" => 'heading',
			"arguments" => array (
				'h2'
			),
			"tags" => array (
				'h2'
			),
			"tooltip" => "Header 2"
		),
		"h3mozilla" => array (
			"visible" => 'false && $.browser.mozilla',
			"className" => 'h3',
			"command" => 'heading',
			"arguments" => array (
				'h3'
			),
			"tags" => array (
				'h3'
			),
			"tooltip" => "Header 3"
		),
		"h4mozilla" => array (
			"visible" => 'false && $.browser.mozilla',
			"className" => 'h4',
			"command" => 'heading',
			"arguments" => array (
				'h4'
			),
			"tags" => array (
				'h4'
			),
			"tooltip" => "Header 4"
		),
		"h5mozilla" => array (
			"visible" => 'false && $.browser.mozilla',
			"className" => 'h5',
			"command" => 'heading',
			"arguments" => array (
				'h5'
			),
			"tags" => array (
				'h5'
			),
			"tooltip" => "Header 5"
		),
		"h6mozilla" => array (
			"visible" => 'false && $.browser.mozilla',
			"className" => 'h6',
			"command" => 'heading',
			"arguments" => array (
				'h6'
			),
			"tags" => array (
				'h6'
			),
			"tooltip" => "Header 6"
		),

		"h1" => array (
			"visible" => 'false && !( $.browser.mozilla )',
			"className" => 'h1',
			"command" => 'formatBlock',
			"arguments" => array (
				'<H1>'
			),
			"tags" => array (
				'h1'
			),
			"tooltip" => "Header 1"
		),
		"h2" => array (
			"visible" => 'false && !( $.browser.mozilla )',
			"className" => 'h2',
			"command" => 'formatBlock',
			"arguments" => array (
				'<H2>'
			),
			"tags" => array (
				'h2'
			),
			"tooltip" => "Header 2"
		),
		"h3" => array (
			"visible" => 'false && !( $.browser.mozilla )',
			"className" => 'h3',
			"command" => 'formatBlock',
			"arguments" => array (
				'<H3>'
			),
			"tags" => array (
				'h3'
			),
			"tooltip" => "Header 3"
		),
		"h4" => array (
			"visible" => 'false && !( $.browser.mozilla )',
			"className" => 'h4',
			"command" => 'formatBlock',
			"arguments" => array (
				'<H4>'
			),
			"tags" => array (
				'h4'
			),
			"tooltip" => "Header 4"
		),
		"h5" => array (
			"visible" => 'false && !( $.browser.mozilla )',
			"className" => 'h5',
			"command" => 'formatBlock',
			"arguments" => array (
				'<H5>'
			),
			"tags" => array (
				'h5'
			),
			"tooltip" => "Header 5"
		),
		"h6" => array (
			"visible" => 'false && !( $.browser.mozilla )',
			"className" => 'h6',
			"command" => 'formatBlock',
			"arguments" => array (
				'<H6>'
			),
			"tags" => array (
				'h6'
			),
			"tooltip" => "Header 6"
		),

		"separator07" => array (
			"visible" => 'true'
		),

		"cut" => array (
			"visible" => 'true'
		),
		"copy" => array (
			"visible" => 'true'
		),
		"paste" => array (
			"visible" => 'true'
		),
		"createLink" => array (
			"visible" => 'false'
		),
		"insertImage" => array (
			"visible" => 'false'
		),
		"separator06" => array (
			"visible" => 'false'
		),
		"strikeThrough" => array (
			"visible" => 'false'
		),
		"removeFormat" => array (
			"visible" => 'false'
		)
	);
	public $model;
	public $attribute;
	public $registerOnly = false;
	public $width = null;
	public $height = null;
	public $right = false;
	public $name = null;
	public $id = null;

	protected function makeOptions() {
		$options['controls'] = $this->controls;

		return CJavaScript::encode($this->controls);
	}

	public function run() {
		$cs = Yii::app()->clientScript;

		$dir = dirname(__FILE__) . DIRECTORY_SEPARATOR;
		$baseUrl = Yii::app()->getAssetManager()->publish($dir);

		if ($this->model == null || $this->attribute == null) {
			$name = $this->name;
			$id = $this->id;
		} else {
			list ($name, $id) = $this->resolveNameID();
		}

		$options = $this->makeOptions();

		$clientScript = Yii::app()->getClientScript();

		$clientScript->registerCssFile($baseUrl . '/js/jquery.wysiwyg.css');
		$clientScript->registerCoreScript('jquery');
		$clientScript->registerScriptFile($baseUrl . '/js/jquery.wysiwyg.js');

		if (!$this->registerOnly && $name != null && $id != null) {

			$js = "var opts = " . $options . ";";
			$js .= "jQuery('#$id')";
			if ($this->width != null){
				$js .= ".css('width','".$this->width."')";
			}
			if ($this->height != null){
				$js .= ".css('height','".$this->height."')";
			}
			if ($this->right){
				$js .= ".css('float','right')";
			}
			$js .= ".wysiwyg(opts);";

			$cs->registerScript('Yii.Jwysiwyg' . $id, $js);
		}
	}

}
?>
